package cn.iocoder.yudao.module.infra.filestore.impl.oss;

import cn.iocoder.yudao.module.infra.filestore.model.CreateFileRes;
import cn.iocoder.yudao.module.infra.filestore.IFileStore;
import cn.iocoder.yudao.module.infra.api.file.FileApi;
import cn.iocoder.yudao.module.infra.api.file.dto.CreateFileResDTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service("fileStore-oss")
public class OSSFileStore implements IFileStore {

    @Resource
    private FileApi fileApi;

    @Override
    public CreateFileRes createFile(String path, byte[] fileContent) {
        final long startTime = System.currentTimeMillis();
        final CreateFileResDTO res = fileApi.onlyCreateFile(null, path, fileContent);
        final long duration = System.currentTimeMillis() - startTime;
        log.info("保存文件到OSS成功, url = {}, duration = {}", res.getUrl(), duration);

        final CreateFileRes createFileRes = CreateFileRes.builder().url(res.getUrl()).encryptKey(res.getEncryptKey()).build();
        return createFileRes;
    }
}
